package com.wandian.setting.mvvm.systempower;

import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.setting.R;
import com.wandian.setting.databinding.SetActivitySystemPowerBinding;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 系统权限
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_SETTING_POWER)
public class SettingsSystemPowerActivity extends  BaseMVVMActivity<SettingsSystemPowerViewModel>  implements EasyPermissions.PermissionCallbacks {
    private SetActivitySystemPowerBinding dataBinding;
    private static final int REQUEST_PERMISSIONS = 0x6601;

    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.set_activity_system_power);
        dataBinding.setLifecycleOwner(this);
        dataBinding.bindToolBar.toolBarTvTitle.setText("权限设置");
        dataBinding.setViewModel(mViewModel);
        initClink();
        checkPermissionAllGranted(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CAMERA,Manifest.permission.MODIFY_AUDIO_SETTINGS});

    }
    private void initClink(){
        dataBinding.bindToolBar.toolBarIvBack.setOnClickListener(v ->   finish());
    }


    private void checkPermissionAllGranted(String[] permissions) {
        for (String permission : permissions) {

            if(permission.equals(Manifest.permission.ACCESS_FINE_LOCATION)){
                dataBinding.bindNextPage1.setLeftText("允许获取位置信息权限");
                if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                    dataBinding.bindNextPage1.setRightText("去设置");
                }else {
                    dataBinding.bindNextPage1.setRightText("已开启");
                }
            }
            if(permission.equals(Manifest.permission.CAMERA)){
                dataBinding.bindNextPage2.setLeftText("允许访问相册权限");
                if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                    dataBinding.bindNextPage2.setRightText("去设置");
                }else {
                    dataBinding.bindNextPage2.setRightText("已开启");
                }
            }
            if(permission.equals(Manifest.permission.MODIFY_AUDIO_SETTINGS)){
                dataBinding.bindNextPage3.setLeftText("允许访问麦克风权限");
                if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                    dataBinding.bindNextPage3.setRightText("去设置");
                }else {
                    dataBinding.bindNextPage3.setRightText("已开启");
                }
            }
        }
        dataBinding.bindNextPage1.rlNextPage.setOnClickListener(v -> {
            String[] perms = {Manifest.permission.ACCESS_FINE_LOCATION};
            if (!EasyPermissions.hasPermissions(this, perms)) {
                EasyPermissions.requestPermissions(this, "为保证APP正常使用，请开启该权限", REQUEST_PERMISSIONS, perms);
            }
        });
        dataBinding.bindNextPage2.rlNextPage.setOnClickListener(v -> {
            String[] perms = {Manifest.permission.CAMERA};
            if (!EasyPermissions.hasPermissions(this, perms)) {
                EasyPermissions.requestPermissions(this, "为保证APP正常使用，请开启该权限", REQUEST_PERMISSIONS, perms);
            }
        });
        dataBinding.bindNextPage3.rlNextPage.setOnClickListener(v -> {
            String[] perms = {Manifest.permission.MODIFY_AUDIO_SETTINGS};
            if (!EasyPermissions.hasPermissions(this, perms)) {
                EasyPermissions.requestPermissions(this, "为保证APP正常使用，请开启该权限", REQUEST_PERMISSIONS, perms);
            }
        });
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // 将结果转发到EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {

        if(!UIUtils.isEmpty(perms)){
            Log.d("onPermissionsGranted",perms.toString());
            if(perms.get(0).equals(Manifest.permission.ACCESS_FINE_LOCATION)){
                dataBinding.bindNextPage1.setRightText("已开启");
            }
            if(perms.get(0).equals(Manifest.permission.CAMERA)){
                dataBinding.bindNextPage2.setRightText("已开启");
            }
            if(perms.get(0).equals(Manifest.permission.MODIFY_AUDIO_SETTINGS)){
                dataBinding.bindNextPage3.setRightText("已开启");
            }
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        if(!UIUtils.isEmpty(perms)){
            Log.d("onPermissionsDenied",perms.toString());
            if(perms.get(0).equals(Manifest.permission.ACCESS_FINE_LOCATION)){
                dataBinding.bindNextPage1.setRightText("去设置");
            }
            if(perms.get(0).equals(Manifest.permission.CAMERA)){
                dataBinding.bindNextPage2.setRightText("去设置");
            }
            if(perms.get(0).equals(Manifest.permission.MODIFY_AUDIO_SETTINGS)){
                dataBinding.bindNextPage3.setRightText("去设置");
            }
        }
    }

}
